import Vue from 'vue'
import Vuex from 'vuex'
import {
    queryBalanceType,
    queryPeriod,
    queryEmpsOfSchoolid,
    queryBalanceItem,
    queryGift,
    querySubject,
    getMyOrgList,
} from '@/api/getData'
import {employeeListApi} from "../api/getData";

Vue.use(Vuex)

const state = {
    loginUser: {},
    //性别
    sexList: [{id: 1, name: "男"}, {id: 2, name: "女"}],
    sexMap:{1:'男',2:'女'},
    stuList:[{id:1,name:"张三"},{id:2,name:"王二"}],
    //学生类型(1,试听 ，2，在读，3 过期)
    stuTypeList: [{id: 1, name: "试听"}, {id: 2, name: "在读"}, {type: 3, name: "过期"}],
    stuTypeMap: {1:"试听",2:"在读",3:"过期"},
    //学生状态(1,未签约，2未排课)
    stuStatusList: [{id: 1, name: "未签约"}, {id: 2, name: "未排课"}],
    stuStatusMap:{1:"未签约",2:"未排课"},
    //微信绑定状态
    wxBindStatusList: [{id: 1, name: "未绑定"}, {id: 2, name: "已绑定"}],

    empTypeList:[{id:1,name:"教务老师"},{id:2,name:"教学老师"}],
    empTypeMap:{1:"教务老师",2:"教学老师"},
    //合约类型(课时卡，时段卡)
    contractTypeList: [{id: 1, name: "课时卡"}, {id: 2, name: "时段卡"}],
    cricleTypeList:[{id:1,name:"按周排课"}],
    //折扣方式
    discountWayList: [],
    //班级状态(0未完结状态，1已完结状态)
    classStatusList: [{id: 1, name: "未完结状态"}, {id: 2, name: "已完结状态"}],
    classStatusMap:{0:"普通状态",1:"未完结状态",2:"已完结状态"},
    //班级类型(1一对一2 普通班级)
    classTypeList: [{id: 1, name: "普通班级"}, {id: 2, name: "一对一"}],
    //科目列表
    subjectList: [{id:1,name:'科目1'},{id:2,name:'科目2'}],
    subjectMap:{0:"科目0",1:'科目1',2:'科目2'},
    //老师列表
    empList: [{id:1,name:'老师1'},{id:2,name:'老师2'}],
    //所在班级
    classList: [{id:1,name:'班级1'},{id:2,name:"班级2"}],
    leaveStatusList:[{id:1,name:"已处理"},{id:2,name:"未处理"},{id:3,name:"已忽略"}],
    leaveStatusMap:[{1:"已处理",2:"未处理",3:"已忽略"}],
    //教室列表
    roomList: [{id:1,name:'教室1'},{id:2,name:'教室2'}],
    //课节状态(0未完结，1已完结)
    periodRecordStatusList: [{id: 1, name: "未完结"}, {id: 2, name: "已完结"}],
    //签到状态列表
    signStatusList: [{id:1,name:'签到'},{id:2,name:'请假'},{id:3,name:'结果'}],
    signStatusMap:{0:"未知",1:"签到",2:"请假",3:"结果"},
    //
    orgList: [{id:1,name:'组织1'},{id:2,name:'组织2'}],
    userinfo: {},
    options: [],
    optionsMenu: {
        "manage": [{"id": 0, "rulepath": "/maintotal", "rulename": "统计", "menu": "manage", "pschoolid": 99999}],
        "usermanage": [{
            "id": 1,
            "rulepath": "/userList/0",
            "rulename": "全部学生",
            "menu": "usermanage",
            "pschoolid": 99999
        }, {"id": 2, "rulepath": "/userList/1", "rulename": "潜在生", "menu": "usermanage", "pschoolid": 99999}, {
            "id": 3,
            "rulepath": "/userList/2",
            "rulename": "正式生",
            "menu": "usermanage",
            "pschoolid": 99999
        }, {"id": 4, "rulepath": "/userList/3", "rulename": "快到期学生", "menu": "usermanage", "pschoolid": 99999}],
        "classmanage": [{
            "id": 11,
            "rulepath": "/allclass/0",
            "rulename": "班级列表",
            "menu": "classmanage",
            "pschoolid": 99999
        }, {"id": 11, "rulepath": "/allclass/1", "rulename": "班课", "menu": "classmanage", "pschoolid": 99999}, {
            "id": 11,
            "rulepath": "/allclass/2",
            "rulename": "一对一",
            "menu": "classmanage",
            "pschoolid": 99999
        }],
        "employeeManage": [{
            "id": 21,
            "rulepath": "/employeeList/1",
            "rulename": "教务老师",
            "menu": "employeeManage",
            "pschoolid": 99999
        }, {"id": 22, "rulepath": "/employeeList/2", "rulename": "教学老师", "menu": "employeeManage", "pschoolid": 99999}],
        "basesetting": [{
            "id": 31,
            "rulepath": "/roomList",
            "rulename": "教室列表",
            "menu": "basesetting",
            "pschoolid": 99999
        }, {
            "id": 32,
            "rulepath": "/subjectList",
            "rulename": "科目列表",
            "menu": "basesetting",
            "pschoolid": 99999
        }, {"id": 33, "rulepath": "/notifySetting", "rulename": "通知设置", "menu": "basesetting", "pschoolid": 99999}],
        "schedulemanage": [{
            "id": 41,
            "rulepath": "/periodRulesList",
            "rulename": "约课列表",
            "menu": "schedulemanage",
            "pschoolid": 99999
        }, {
            "id": 32,
            "rulepath": "/periodRecordList",
            "rulename": "约课课节",
            "menu": "schedulemanage",
            "pschoolid": 99999
        }],
        "leavemanage": [{
            "id": 51,
            "rulepath": "/leaveRequestList",
            "rulename": "请假列表",
            "menu": "leavemanage",
            "pschoolid": 99999
        }]
    },
    currentOrgId: 0,
    ruleList: [{id:1,name:'添加学生'},{id:2,name:'删除学生'},{id:3,name:'修改学生'},{id:4,name:'添加班级'}],

}
const getters = {
    optionsMenu: state => state.optionsMenu,
    loginUser: state => state.loginUser,
    gorgList: state => state.orgList,
    gsexList: state => state.sexList,
    gsexMap:state=>state.sexMap,
    gempTypeList: state=>state.empTypeList,
    gempTypeMap:state=>state.empTypeMap,
    gstuTypeList: state => state.stuTypeList,
    gstuTypeMap:state=>state.stuTypeMap,
    gstuStatusList: state => state.stuStatusList,
    gstuStatusMap:state=>state.stuStatusMap,
    gwxBindStatusList: state => state.wxBindStatusList,
    gcontractTypeList: state => state.contractTypeList,
    discountWayList: state => state.discountWayList,
    gclassStatusList: state => state.classStatusList,
    gclassTypeList: state => state.classTypeList,
    gsubjectList: state => state.subjectList,
    gsubjectMap:state=>state.subjectMap,
    groomList: state => state.roomList,
    gcricleTypeList:state=>state.cricleTypeList,
    gperiodRecordStatusList: state => state.periodRecordStatusList,
    gsignStatusList: state => state.signStatusList,
    userinfo: state => state.userinfo,
    options: state => state.options,
    optionMenu: state => state.optionsMenu,
    gclassList: state => state.classList,
    gempList: state => state.empList,
    gstuList:state=>state.stuList,
    gleaveStatusList:state=>state.leaveStatusList,
    gleaveStatusMap:state=>state.leaveStatusMap,
    getCurrentOrgId: state => state.currentOrgId,
    ruleListGlobal: state => state.ruleList,
}

const mutations = {
    saveAdminInfo(state, data) {
        this.state.userinfo = data;
        localStorage.setItem("adminInfo", JSON.stringify(res.data));

    },
    setCurrentOrgId(state) {
        this.state.currentOrgId = this.orgList[0];
    },
    setOrgList(state, data) {
        this.state.orgList = data;
    },

    setRuleList(state, data) {
        this.state.ruleList = data;
    },

    setClassList(state, data) {
        this.state.classList = data;
    },
    setEmpList(state, data) {
        this.state.empList = data;
    },

    setLoginUser(state, data) {
        this.state.loginUser = data;
    },
    setSexList(state, data) {
        this.state.sexList = data;
    },
    setStuTypeList(state, data) {
        this.state.f = data;
    },
    setWxBindStatusList(state, data) {
        this.state.wxBindStatusList = data;

    },
    setContractTypeList(state, data) {
        this.state.contractTypeList = data;

    },
    setDiscountWayList(state, data) {
        this.state.discountWayList = data;

    },
    setClassStatusList(state, data) {
        this.state.classStatusList = data;

    },
    setClassTypeList(state, data) {
        this.state.classTypeList = data;

    },
    setSubjectList(state, data) {
        this.state.subjectList = data;

    },
    setRoomList(state, data) {
        this.state.roomList = data;

    },
    setPeriodRecordStatusList(state, data) {
        this.state.periodRecordStatusList = data;

    },
    setSignStatusList(state, data) {
        this.state.signStatusList = data;

    },


    add_tabs(state, data) {
        this.state.options.push(data);
    },

    setMenuRles(state, data) {
        this.state.optionsMenu = data;
    },


    add_manytabs(state, data) {
        var opts = [];
        this.state.options = [];
        data.forEach(item => {
            opts.push(item);
            this.state.options.push(item);
        });
        localStorage.setItem("options", JSON.stringify(opts));
    },
    // 删除tabs
    delete_tabs(state, route) {
        let index = 0;
        for (let option of state.options) {
            if (option.route === route) {
                break;
            }
            index++;
        }
        this.state.options.splice(index, 1);
    },
    // 设置当前激活的tab
    set_active_index(state, index) {
        this.state.activeIndex = index;
    },


}

const actions = {

    async getMyOrgList({commit}) {
        try {
            const res = await getMyOrgList()
            if (res.success == true) {
                commit('orgList', res.data);
            } else {
                throw new Error(res)
            }
        } catch (err) {
            console.log('get my orgList error')
        }
    },
    async getCommonConfig({commit}) {
        try {
            const res = await getBusConfig()
            if (res.success == true) {
                commit('setClassTypeList', res.data.get('classType'));
                commit('setClassStatusList', res.data.get('classStatus'));
                this.commit('setWxBindStatusList', null);
                commit('setContractTypeList', null);
                commit('setDiscountWayList', null);
                commit('setPeriodRecordStatusList', null);
                commit('setSignStatusList', null);
                commit('setRuleList', null);
            } else {
                throw new Error(res)
            }
        } catch (err) {
            console.log('get common config error')
        }
    },


    async getAdminData({commit}) {
        try {
            const res = await getAdminInfo()
            if (res.success == true) {
                commit('saveAdminInfo', res.data);
            } else {
                throw new Error(res)
            }
        } catch (err) {
            console.log('您尚未登陆或者session失效')
        }
    },


}

export default new Vuex.Store({
    state,
    actions,
    mutations,
    getters,
})
